package co.edu.uniquindio.ingesis.GUI;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;

import javax.swing.JButton;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

import co.edu.uniquindio.ingesis.logica.HiloPelota;
import co.edu.uniquindio.ingesis.logica.Pelota;
import co.edu.uniquindio.ingesis.logica.Tablero;


/**
 * This code was edited or generated using CloudGarden's Jigloo
 * SWT/Swing GUI Builder, which is free for non-commercial
 * use. If Jigloo is being used commercially (ie, by a corporation,
 * company or business for any purpose whatever) then you
 * should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details.
 * Use of Jigloo implies acceptance of these licensing terms.
 * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
 * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
 * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class VentanaPrincipal extends javax.swing.JFrame implements ActionListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	protected Tablero elTablero;
	private JButton botonParar;
	private JButton botonAgregar;
	protected HiloPelota laPelota;
	/**
	 * Auto-generated main method to display this JFrame
	 */
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				VentanaPrincipal inst = new VentanaPrincipal();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}

	public VentanaPrincipal() {
		super();
		
		elTablero= new Tablero();
		elTablero.iniciarHilos();
		agregarpelotas();
		initGUI();
	}
    
	public void agregarpelotas()
	{
		LinkedList<Pelota> pelotas = elTablero.getListaPelotas();
		for (int i = 0; i < pelotas.size(); i++)
		{
			getContentPane().add(pelotas.get(i));
			pelotas.get(i).setVisible(true);
		}
		elTablero.iniciarHilos();
	}
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			getContentPane().setBackground(new java.awt.Color(255,255,255));
			//			
			{
				botonAgregar = new JButton();
				getContentPane().add(botonAgregar);
				botonAgregar.setText("Nueva Pelota");
				botonAgregar.setBounds(70, 577, 125, 36);
				botonAgregar.addActionListener(this);
			}
			{
				botonParar = new JButton();
				getContentPane().add(botonParar);
				botonParar.setText("Detener");
				botonParar.setBounds(332, 577, 115, 36);
				botonParar.addActionListener(this);
			}
			pack();
			this.setSize(550, 662);
		} catch (Exception e) {
			//add your error handling code here
			e.printStackTrace();
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) 
	{
		// TODO Auto-generated method stub
		if (e.getSource()== botonAgregar)
		{
			elTablero.nuevaPelota();
			agregarpelotas();
			//elTablero.nuevaPelota();
			
		}
		if (e.getSource()==botonParar)
		{
			elTablero.finalizarHilos();
		}
	}

}
